package com.leha.api.controller;

import com.leha.core.entity.mybatis.LhUser;
import com.leha.core.entity.vo.UserLoginVO;
import com.leha.core.services.GameServices;
import com.leha.core.services.GameZZLDSServices;
import com.leha.core.services.UserServices;
import com.leha.core.services.ErrorMessageServices;
import constants.APIErrorDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import utils.APIResult;

import javax.ws.rs.FormParam;
import javax.ws.rs.QueryParam;

@RestController
@Api(value = "/user", description = "用户相关API类")
@RequestMapping("/user")
@ResponseStatus(HttpStatus.OK)

public class UserController {
    @Autowired
    private UserServices userServices;
    @Autowired
    private ErrorMessageServices errorMessageServices;
    @Autowired
    private GameServices gameServices;

    @RequestMapping(value = "/{gameId}/{uid}/update", method = {RequestMethod.POST})
    @ApiOperation(value = "上传用户资料", notes = "传输用户资料")
    APIResult login(@FormParam(value = "sessionKey") String sessionKey,
                    @FormParam(value = "encryptedData") String encryptedData,
                    @FormParam(value = "iv") String iv,
                    @PathVariable(value = "uid") Integer uid,
                    @PathVariable(value = "gameId") Integer gameId) {
        if(sessionKey==null || sessionKey.isEmpty() || encryptedData==null || encryptedData.isEmpty() || iv==null ||iv.isEmpty()) {
            return new APIResult(APIErrorDict.SESSION_KEY);
        }
        UserLoginVO loginUser = userServices.SaveInfo(gameId, uid, sessionKey, encryptedData, iv);
        if (loginUser == null) {
            return new APIResult(errorMessageServices.getLastError());
        } else {
            return new APIResult(loginUser, APIResult.ReturnStrategy.NONE_NULL);
        }
    }

    @RequestMapping(value = "/{gameId}/enter", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "登录", notes = "直接传code，后台获取openid，匹配对应UID")
    APIResult loginV2(@FormParam(value = "code") String code,
                    @PathVariable(value = "gameId") Integer gameId) {
        if (code == null) {
            return new APIResult(APIErrorDict.PARAMERROR);
        }
        UserLoginVO loginUser = userServices.enter(gameId, code);
        if (loginUser == null) {
            return new APIResult(errorMessageServices.getLastError());
        } else {
            return new APIResult(loginUser, APIResult.ReturnStrategy.NONE_NULL);
        }
    }

    @RequestMapping(value = "/{uid}", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "更新信息", notes = "再次刷新主页信息")
    APIResult loginV2(@PathVariable(value="uid") Integer uid) {
        UserLoginVO loginUser = userServices.getUserInfo(uid);
        if (loginUser == null) {
            return new APIResult(errorMessageServices.getLastError());
        } else {
            return new APIResult(loginUser, APIResult.ReturnStrategy.NONE_NULL);
        }
    }

    @RequestMapping(value = "/{gameId}/{uid}/rewardsList", method = {RequestMethod.GET})
    @ApiOperation(value = "获取玩家的游戏奖励", notes = "玩家的奖励")
    APIResult GetGameRewardsList(@PathVariable Integer uid,
                                 @PathVariable Integer gameId,
                                 @QueryParam("pageNum") Integer pageNum,
                                 @QueryParam("pageSize") Integer pageSize){
        return new APIResult(gameServices.getMyRewards(gameId, uid, pageNum, pageSize));
    }

    @RequestMapping(value = "/{gameId}/{uid}/share", method = {RequestMethod.GET})
    @ApiOperation(value = "玩家分享", notes = "玩家分享获得游戏次数")
    APIResult UserShare(@PathVariable Integer uid,
                                 @PathVariable Integer gameId,
                                 @FormParam(value = "sessionKey") String sessionKey,
                                 @FormParam(value = "encryptedData") String encryptedData,
                                 @FormParam(value = "iv") String iv){
        if(sessionKey==null || sessionKey.isEmpty() || encryptedData==null || encryptedData.isEmpty() || iv==null ||iv.isEmpty()) {
            return new APIResult(APIErrorDict.SESSION_KEY);
        }
        return new APIResult(gameServices.userShare(gameId, uid, sessionKey, encryptedData, iv));
    }

    @RequestMapping(value = "/{gameId}/{uid}/exchange", method = {RequestMethod.POST})
    @ApiOperation(value = "玩家兑换奖品", notes = "玩家分享获得游戏次数")
    APIResult GetExchangeRewards(@PathVariable Integer uid,
                                 @PathVariable Integer gameId,
                                 @FormParam("name") String name,
                                 @FormParam("phone") String phone,
                                 @FormParam("address") String address,
                                 @FormParam("email") String email,
                                 @FormParam("recId") Integer recId,
                                 @FormParam("itemId") Integer itemId){
        return new APIResult(gameServices.userExchange(gameId, uid, recId, itemId, name, phone, address, email));
    }
}
